//
//  MeViewController.m
//  Article
//
//  Created by LONG on 16/4/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MeViewController.h"
#import "MyInfoViewController.h"
#import "PurseViewController.h"
#import "FundDataViewController.h"
#import "InviteFriendsViewController.h"
#import "AboutViewController.h"
#import "MaterialViewController.h"
#import "PaymentViewController.h"
#import "MyPaymentViewController.h"

#import "UIView+Toast.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"
#import "UIKit+AFNetworking.h"
#import "AFNetworking.h"
#import "MessageCenterViewController.h"
#import "UITabBar+NumberOfTabbar.h"

#import "GameListCell.h"

@interface MeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_collectionView;
    UIButton *_rightbtn;
    UIImageView *_redImage;
    
}
@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)UIImageView *headImageView;//头像
@property (nonatomic,strong)UILabel *nameLabel;//昵称

@property (nonatomic,strong)MyInfoViewController *myInfoVC;


@end

@implementation MeViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    
    [self createUI];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([[user objectForKey:@"isMessage"] isEqualToString:@"YES"]) {
        _redImage = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0, 6, 6)];
        _redImage.backgroundColor = [UIColor redColor];
        _redImage.layer.cornerRadius = _redImage.frame.size.width / 2;
        _redImage.layer.masksToBounds = YES;
        [_rightbtn addSubview:_redImage];
    }else if ([[user objectForKey:@"isMessage"] isEqualToString:@"NO"]){
        [_redImage removeFromSuperview];
    }
    //小红点消失
    [self.tabBarController.tabBar hideBadgeOnItemIndex:3];
    [_myInfoVC returnInfoBlock:^(UIImage *headImage, NSString *nameLabelStr) {
        
        
        _headImageView.image = headImage;
        
        if ([[user objectForKey:@"agents_level"] isEqualToString:@"0"]) {
            
            _nameLabel.text = [NSString stringWithFormat:@"%@",nameLabelStr];
            
        }else{
            _nameLabel.text = [NSString stringWithFormat:@"%@  %@",nameLabelStr,@"\U0000e60e"];
        }
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _myInfoVC = [[MyInfoViewController alloc]init];
    _dataArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    
    [self initNavigation];
    [self initCollectionView];
}
-(void)initNavigation{
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:18],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.navigationItem.title = @"";
    
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController.navigationBar setBarTintColor:UIColorFromRGB(MYColor)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate =(id)self;
    
    //nav底部线消失
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    
    //点我进入消息页面
     _rightbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    _rightbtn.titleLabel.font = [UIFont fontWithName:@"IconFont" size: 28 * MYWIDTH];
    [_rightbtn setTitle:@"\U0000e608" forState:UIControlStateNormal];
    [_rightbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [_rightbtn addTarget:self action:@selector(message) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithCustomView:_rightbtn];
    self.navigationItem.rightBarButtonItem = right;
    [self.navigationItem.rightBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
}
-(void)message{
    MessageCenterViewController *messageVC = [[MessageCenterViewController alloc]init];
    [self.navigationController pushViewController:messageVC animated:YES];
}

-(void)createUI{
    
    NSUserDefaults *user = [[NSUserDefaults  alloc]init];
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 150 *MYHEIGHT)];
    headView.backgroundColor = UIColorFromRGB(MYColor);
    headView.userInteractionEnabled = YES;
    [self.view addSubview:headView];
    
    _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(295/2 *MYWIDTH, 0, 80*MYHEIGHT, 80 *MYHEIGHT)];
    [_headImageView sd_setImageWithURL:[NSURL URLWithString:[user objectForKey:@"imageurl"]] placeholderImage:[UIImage imageNamed:@"头像"]];
    _headImageView.layer.cornerRadius = 40 *MYHEIGHT;
    _headImageView.layer.masksToBounds = YES;
    _headImageView.userInteractionEnabled = YES;
    [headView addSubview:_headImageView];
    
    
    UITapGestureRecognizer *headTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headClick:)];
    [_headImageView addGestureRecognizer:headTap];
    
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 90 *MYHEIGHT, WIDTH, 40)];
    _nameLabel.font = [UIFont fontWithName:@"IconFont" size: 18 * MYWIDTH];
    
    if ([[user objectForKey:@"agents_level"] isEqualToString:@"0"]) {
       
        _nameLabel.text = [NSString stringWithFormat:@"%@",[user objectForKey:@"nickname"]];

    }else{
         _nameLabel.text = [NSString stringWithFormat:@"%@  %@",[user objectForKey:@"nickname"],@"\U0000e60e"];
    }
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    //_nameLabel.font = [UIFont fontWithName:@"Heiti SC" size:14 *MYWIDTH];
    [headView addSubview:_nameLabel];
    
    
    
}
- (void)initCollectionView{
    UICollectionViewFlowLayout *layout =
    [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing =10;
    layout.sectionInset= UIEdgeInsetsMake(0, 0, 0, 0);

    
    _collectionView = [[UICollectionView alloc]
                       initWithFrame:CGRectMake(0, 150 *MYHEIGHT+10, WIDTH,
                                                (WIDTH-40)/3*2+10)
                       collectionViewLayout:layout];
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.bounces = YES;
    [_collectionView setScrollEnabled:NO];
    _collectionView.backgroundColor = [UIColor whiteColor];
    //_collectionView.backgroundColor = UIColorFromRGB(0xefeff1);
    _collectionView.showsVerticalScrollIndicator = NO;
    
    [_collectionView registerClass:[GameListCell class]
        forCellWithReuseIdentifier:@"homeMenuCell"];
    
    
    [self.view addSubview:_collectionView];
    
    
}
-(void)headClick:(UITapGestureRecognizer *)headTap{
    [self.navigationController pushViewController:_myInfoVC animated:YES];
}
#pragma mark - CollectionView Delegate

- (NSInteger)collectionView:(UICollectionView *)collectionView
     numberOfItemsInSection:(NSInteger)section {
    //    return _menuSource.items.count;
    return 6;
}


- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    return CGSizeMake((WIDTH-40)/3, (WIDTH-40)/3+5);
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GameListCell *cell =
    [collectionView dequeueReusableCellWithReuseIdentifier:@"homeMenuCell"
                                              forIndexPath:indexPath];
    //    NSArray *coverImgarr = @[@"钻石",@"二维码",@"金币",@"团队",@"提现",@"关于我们"];
    //    cell.coverImg.image = [UIImage imageNamed:coverImgarr[indexPath.row]];
    NSArray *imagetitle = @[@"\U0000e603",@"\U0000e601",@"\U0000e600",@"\U0000e602",@"\U0000e606",@"\U0000e604"];
    cell.imagelab.text = [imagetitle objectAtIndex:indexPath.row];
    NSArray *colorarr = @[UIColorFromRGB(0xF06572),UIColorFromRGB(0x5CC6F1),UIColorFromRGB(0xF9DC41),UIColorFromRGB(0x5CC6F1),UIColorFromRGB(0x3BC647),UIColorFromRGB(0xA020F0)];
    cell.imagelab.font = [UIFont fontWithName:@"IconFont" size: 35 * MYWIDTH];
    cell.imagelab.textColor = [UIColor whiteColor];
    cell.imagelab.textAlignment = NSTextAlignmentCenter;
    cell.imagelab.backgroundColor = colorarr[indexPath.row];
    
    
    
    
    NSArray *titlearr = @[@"成为代理",@"推广赚钱",@"我的佣金",@"我的团队",@"提现",@"关于我们"];
    cell.lable.text  = titlearr[indexPath.row];
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSUserDefaults *user = [[NSUserDefaults  alloc]init];

    switch (indexPath.row) {
        case 0:{
            NSLog(@">>>>>>%@",[user objectForKey:@"auditing"]);
            if ([[user objectForKey:@"auditing"] intValue] == 1) {
                PaymentViewController *payment = [[PaymentViewController alloc]init];
                [self.navigationController pushViewController:payment animated:YES];
                
            }else{
               
                MyPaymentViewController *MyPayment = [[MyPaymentViewController alloc]init];
                [self.navigationController pushViewController:MyPayment animated:YES];
            }
        
            

            break;
        }

        case 1:{
//            if ([[user objectForKey:@"agents_level"] isEqualToString:@"0"]) {
//                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"您还不是代理\r不能进行推广赚钱" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
//                [alert show];
//            }else{
                InviteFriendsViewController *Invite = [[InviteFriendsViewController alloc]init];
                [self.navigationController pushViewController:Invite animated:YES];
            //}
            
            break;
        }
        case 2:{
            FundDataViewController *Fund = [[FundDataViewController alloc]init];
            [self.navigationController pushViewController:Fund animated:YES];
            break;
        }

        case 3:{
            MaterialViewController *mater = [[MaterialViewController alloc]init];
            [self.navigationController pushViewController:mater animated:YES];
            break;
        }

        case 4:{
            PurseViewController *Cash = [[PurseViewController alloc]init];
            [self.navigationController pushViewController:Cash animated:YES];
            break;
        }
        case 5:{
            AboutViewController *about = [[AboutViewController alloc]init];
            [self.navigationController pushViewController:about animated:YES];
            break;
        }
         default:
            break;
    }
    
}


@end
